<?php
namespace app\member\controller;
use app\member\controller\BaseAdmin;
class Manager extends BaseAdmin{
    public function index(){
       $manager = getSession("manager");
       $this->assign("manager",$manager);
       return $this->fetch(); 
    }
    public function main(){
        return $this->fetch();
    }
    public function student(){
        $where = ['role'=>0];
        if (input("tid")){
            $where['tid'] = input("tid");
        }
        $students = db("member")->where($where)->select();
        $this->assign("students",$students);
        return $this->fetch();
    }
    public function addstudent(){
        $coach = db("member")->field("id,name")->where(['role'=>1])->select();
        $this->assign("coachs",$coach);
        return $this->fetch();
    }
    
    public function editstudent($id){
        $coach = db("member")->field("id,name")->where(['role'=>1])->select();
        $this->assign("coachs",$coach);
        
        $student = db("member")->find($id);
        $this->assign("student",$student);
        
        $status = [
            ['id'=>0,'name'=>'待审核'],
            ['id'=>1,'name'=>'审核通过'],
            ['id'=>-1,'name'=>'审核未通过'],
        ];
        $this->assign("status",$status);
        return $this->fetch();
    }
    
    public function coach(){
        $coachs = db("member")->where(['role'=>1])->select();
        $this->assign("coachs",$coachs);
        return $this->fetch();
    }
    
    public function addcoach(){
        return $this->fetch();
    }
    
    public function editcoach($id){
        $coach = db("member")->find($id);
        $this->assign("coach",$coach);
        
        $status = [
            ['id'=>0,'name'=>'待审核'],
            ['id'=>1,'name'=>'审核通过'],
            ['id'=>-1,'name'=>'审核未通过'],
        ];
        $this->assign("status",$status);
        
        return $this->fetch();
    }
    
    public function addmember(){
        if (request()->isPost()){
            $file = request()->file("headpic");
            $savename = null;
            if (!empty($file)){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info){
                    $savename = $info->getSavename();
                }else{
                    $this->error($file->getError());
                }
            }
            $data = input();
            $data['headerpic'] = $savename;
            $data['password'] = md5("111111");
            $data['create_time'] = time();
            $res = db('member')->insert($data);
            if ($res){
                $this->success("添加成功！");
            }else{
                $this->error("添加失败！");
            }
        }else{
            die("非法访问！");
        }
    }
    
    public function del($id){
        $res = db("member")->delete($id);
        if ($res){
            $this->success("删除成功！");
        }else{
            $this->error("删除失败！");
        }
    }
    
    
    public function editmember(){
        if (request()->isPost()){
            $file = request()->file("headpic");
            $savename = null;
            if (!empty($file)){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info){
                    $savename = $info->getSavename();
                }else{
                    $this->error($file->getError());
                }
            }
            $data = input();
            $data['headerpic'] = $savename;
            if ($data['password']){
                $data['password'] = md5($data['password']);
            }
            $data['create_time'] = time();
            if (empty($data['password'])){
                unset($data['password']);
            }
            $res = db('member')->update($data);
            if ($res){
                $this->success("编辑成功！");
            }else{
                $this->error("编辑失败！");
            }
        }else{
            die("非法访问！");
        }
    }
}